package cobersky.skinUI.core
{
	import cobersky.skinUI.layout.Layout;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class Container extends Component
	{
		protected var _containerSprite:Sprite;
		protected var _layout:Layout;
		protected var _numElements:uint;
		protected var _hasMask:Boolean;
		protected var _scrollRect:Rectangle;
		public function Container(){
			init();
		}
		protected function init():void{
		}
		public function getElementAt(index:uint):Component{
			return _containerSprite.getChildAt(index) as Component;
		}
		
		public function get numElements():uint
		{
			return _containerSprite.numChildren;
		}

		public function get layout():Layout
		{
			return _layout;
		}

		public function set layout(value:Layout):void
		{
			_layout = value;
			//if(_elementsReady){
			_layout.doLayout(this);
			//}
		}

		public function get containerHeight():Number
		{
			return _scrollRect.height;
		}

		public function set containerHeight(value:Number):void
		{
			_scrollRect.height = value;
			if(_hasMask)_containerSprite.scrollRect=_scrollRect;
		}

		public function get containerWidth():Number
		{
			return _scrollRect.width;
		}

		public function set containerWidth(value:Number):void
		{
			_scrollRect.width = value;
			if(_hasMask)_containerSprite.scrollRect=_scrollRect;
		}

		public function set hasMask(value:Boolean):void
		{
			if(value==_hasMask)return;
			_hasMask = value;
			if(_hasMask)this._containerSprite.scrollRect=_scrollRect;
			else	this._containerSprite.scrollRect=null;
		}

		public function addElement(element:Component):uint{
			element.create();
			if(_layout){
				_layout.layoutElement(element,numElements);
			}
			_containerSprite.addChild(element);
			return numElements;
		}
		public function removeElement(element:Component):Component{
			return this._containerSprite.removeChild(element) as Component;
		}
		public function removeElementAt(index:uint):Component{
			return _containerSprite.removeChildAt(index) as Component;
		}
		override public function dispose():void
		{
			var i:uint=this.numElements;
			while(--i){
				this.removeElementAt(i).dispose();
			}
			super.dispose();
		}
	}
}